// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "usb_descriptors.h"
#include "sdkconfig.h"

#define USB_TUSB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4))

/**** TinyUSB default ****/
tusb_desc_device_t descriptor_tinyusb = {
	.bLength = sizeof(descriptor_tinyusb),
	.bDescriptorType = TUSB_DESC_DEVICE,
	.bcdUSB = 0x0220,

#if CFG_TUD_CDC
	// Use Interface Association Descriptor (IAD) for CDC
	// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
	.bDeviceClass = TUSB_CLASS_MISC,
	.bDeviceSubClass = MISC_SUBCLASS_COMMON,
	.bDeviceProtocol = MISC_PROTOCOL_IAD,
#else
	.bDeviceClass = 0x00,
	.bDeviceSubClass = 0x00,
	.bDeviceProtocol = 0x00,
#endif

	.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,

	.idVendor = 0xCafe,
	.idProduct = USB_TUSB_PID,
	.bcdDevice = 0x0210,

	.iManufacturer = 0x01,
	.iProduct = 0x02,
	.iSerialNumber = 0x03,

	.bNumConfigurations = 0x01};

tusb_desc_strarray_device_t descriptor_str_tinyusb = {
	// array of pointer to string descriptors
	(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
	"TinyUSB",			  // 1: Manufacturer
	"TinyUSB Device",	  // 2: Product
	"996_996_996",		  // 3: Serials, should use chip ID
	"TinyUSB CDC",		  // 4: CDC Interface
	"TinyUSB MSC",		  // 5: MSC Interface
	"TinyUSB HID",		  // 6: HID
	"TinyUSB WEBUSB"	  // 7: WEBUSB
};
/* End of TinyUSB default */

/**** Kconfig driven Descriptor ****/
tusb_desc_device_t descriptor_kconfig = {
	.bLength = sizeof(descriptor_kconfig),
	.bDescriptorType = TUSB_DESC_DEVICE,
	.bcdUSB = 0x0220,

#if CFG_TUD_CDC
	// Use Interface Association Descriptor (IAD) for CDC
	// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
	.bDeviceClass = TUSB_CLASS_MISC,
	.bDeviceSubClass = MISC_SUBCLASS_COMMON,
	.bDeviceProtocol = MISC_PROTOCOL_IAD,
#else
	.bDeviceClass = 0x00,
	.bDeviceSubClass = 0x00,
	.bDeviceProtocol = 0x00,
#endif

	.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,

#if CONFIG_USB_DESC_USE_ESPRESSIF_VID
	.idVendor = USB_ESPRESSIF_VID,
#else
	.idVendor = CONFIG_USB_DESC_CUSTOM_VID,
#endif

#if CONFIG_USB_DESC_USE_DEFAULT_PID
	.idProduct = USB_TUSB_PID,
#else
	.idProduct = CONFIG_USB_DESC_CUSTOM_PID,
#endif

	.bcdDevice = CONFIG_USB_DESC_BCDDEVICE,

	.iManufacturer = 0x01,
	.iProduct = 0x02,
	.iSerialNumber = 0x03,

	.bNumConfigurations = 0x01};

tusb_desc_strarray_device_t descriptor_str_kconfig = {
	// array of pointer to string descriptors
	(char[]){0x09, 0x04},				 // 0: is supported language is English (0x0409)
	CONFIG_USB_DESC_MANUFACTURER_STRING, // 1: Manufacturer
	CONFIG_USB_DESC_PRODUCT_STRING,		 // 2: Product
	CONFIG_USB_DESC_SERIAL_STRING,		 // 3: Serials, should use chip ID

#if CONFIG_USB_CDC_ENABLED
	CONFIG_USB_DESC_CDC_STRING, // 4: CDC Interface
#else
	"",
#endif

#if CONFIG_USB_MSC_ENABLED
	CONFIG_USB_DESC_MSC_STRING, // 5: MSC Interface
#else
	"",
#endif
#if CONFIG_USB_HID_ENABLED
	CONFIG_USB_DESC_HID_STRING, // 6: HIDs
#else
	"",
#endif
#if CONFIG_USB_VENDOR_ENABLED
	CONFIG_USB_DESC_VENDOR_STRING, // 7: HIDs
#else
	"",
#endif

};
/* End of Kconfig driven Descriptor */